package com.zjdiepu.www.rest.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.ParentModel;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.commpent.mapper.dh.DzRegistrationVoMapper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessCarVoMapper;
import com.zjdiepu.www.ibiz.dz.IDzPickCarService;
import com.zjdiepu.www.ibiz.dz.IDzRegistrationService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dz.dto.DzPickCar;
import com.zjdiepu.www.model.dz.dto.DzRegistration;
import com.zjdiepu.www.model.dz.request.DzRegistrationRequest;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.request.YwBusinessCarRequest;
import com.zjdiepu.www.model.yw.response.YwBusinessCarResponse;

/**
 * 车辆信息Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 19时30分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "YwBusinessCarController", produces="application/json;charset=UTF-8",method = RequestMethod.POST)
@Api(tags = {"提车/车牌"}, produces="application/json;charset=UTF-8")
public class YwBusinessCarController extends BaseController {
	@Resource
	private TokenUserHelper tokenUserHelper;
    @Resource
    private IYwBusinessCarService ywBusinessCarService;
    @Resource
    private IDzPickCarService dzPickCarService;
    @Resource
    private IDzRegistrationService dzRegistrationService;
    @Resource
    private YwBusinessCarVoMapper ywBusinessCarVoMapper;
    @Resource
    private DzRegistrationVoMapper dzRegistrationVoMapper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: liftConfirmation  <br/>
     * @Description: 提车 <br/>
     * @param ywBusinessCarRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "提车", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("liftConfirmation")
    public ApiResponse liftConfirmation(@Validated(Add.class) @RequestBody YwBusinessCarRequest ywBusinessCarRequest) {
    	SysUser currentUser = tokenUserHelper.getUser();
    	DzPickCar dzPickCar = new DzPickCar();
		BeanUtils.copyProperties(ywBusinessCarRequest, dzPickCar);
        return dzPickCarService.saveConfirmation(dzPickCar, currentUser);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: getPlates  <br/>
     * @Description: 获取车牌 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "获取车牌", response = DzRegistration.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getPlates")
    public ApiResponse getPlates(@Validated(Update.class) @RequestBody ParentModel parentModel) {
    	tokenUserHelper.getUser();
    	DzRegistration dzRegistration = new DzRegistration();
    	dzRegistration.setBusiId(parentModel.getId());
    	dzRegistration.setDpStatus(DpStatus.NORMAL.getCode());
    	dzRegistration = dzRegistrationService.selectRefined(dzRegistration);
    	dzRegistrationVoMapper.mapper(dzRegistration);
        return ApiResponse.success(dzRegistration);
    }
    
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: registrationPlates  <br/>
     * @Description: 车牌登记 <br/>
     * @param dzRegistrationRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "车牌登记", response = Boolean.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("registrationPlates")
    public ApiResponse registrationPlates(@Validated(Add.class) @RequestBody DzRegistrationRequest dzRegistrationRequest) {
    	SysUser currentUser = tokenUserHelper.getUser();
    	DzRegistration dzRegistration = new DzRegistration();
		BeanUtils.copyProperties(dzRegistrationRequest, dzRegistration);
        return dzRegistrationService.saveRegistrationPlates(dzRegistration, currentUser);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 新车提车记录 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新车提车记录", response = YwBusinessCarResponse.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getById")
    public ApiResponse getById(@Validated(Update.class) @RequestBody ParentModel parentModel) {
    	tokenUserHelper.getUser();
    	YwBusinessCar ywBusinessCar = ywBusinessCarService.selectById(parentModel.getId());
    	if(null == ywBusinessCar) {
    		return ApiResponse.fail(ResultFlagType.MISSING);
    	}
    	Double oldPrice = ywBusinessCar.getPrice();
    	String busiId = ywBusinessCar.getBusiId();
    	if(StringUtils.isBlank(busiId)) {
    		return ApiResponse.fail("999","业务记录ID缺失");
    	}
    	//新车提车
    	DzPickCar dzPickCar = new DzPickCar();
    	dzPickCar.setBusiId(busiId);
    	dzPickCar.setDpStatus(DpStatus.NORMAL.getCode());
    	dzPickCar = dzPickCarService.selectRefined(dzPickCar);
    	if(null != dzPickCar) {
    		ywBusinessCar.setPickCarId(dzPickCar.getId());
    		ywBusinessCar.setPickCarTime(dzPickCar.getPickCarTime());
    		BeanUtils.copyProperties(dzPickCar, ywBusinessCar); //使用新值
    	}
    	ywBusinessCar.setPrice(oldPrice);//使用老值
    	ywBusinessCarVoMapper.mapper(ywBusinessCar);
    	YwBusinessCarResponse ywBusinessCarResponse = new YwBusinessCarResponse();
    	BeanUtils.copyProperties(ywBusinessCar, ywBusinessCarResponse);
    	ywBusinessCarResponse.setNameCategoryCar(ywBusinessCar.getCategoryCarDisplay());
    	ywBusinessCarResponse.setNameColorCar(ywBusinessCar.getColorCarDisplay());
        return ApiResponse.success(ywBusinessCarResponse);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: getByCarId  <br/>
     * @Description: 获取车辆信息 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "获取车辆信息", response = YwBusinessCar.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getByCarId")
    public ApiResponse getByCarId(@Validated(Update.class) @RequestBody ParentModel parentModel) {
    	tokenUserHelper.getUser();
    	YwBusinessCar ywBusinessCar = ywBusinessCarService.selectById(parentModel.getId());
    	ywBusinessCarVoMapper.mapper(ywBusinessCar);
        return ApiResponse.success(ywBusinessCar);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: saveCar  <br/>
     * @Description: 保存车辆信息 <br/>
     * @param ywBusinessCar
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "保存车辆信息", response = String.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("saveCar")
    public ApiResponse saveCar(@Validated(Add.class) @RequestBody YwBusinessCar ywBusinessCar) {
    	SysUser user = tokenUserHelper.getUser();
    	ywBusinessCar.setModifier(user.getId()); //统一使用修改人和修改人名称来初始化
    	ywBusinessCar.setModifierName(user.getName());
    	String id = ywBusinessCar.getId();
    	if(StringUtils.isNotBlank(id)) {
    		ywBusinessCarService.saveUpdateAndDelete(ywBusinessCar, OperateType.edit);
    	} else {
    		ywBusinessCarService.saveUpdateAndDelete(ywBusinessCar, OperateType.add);
    	}
        return ApiResponse.success(ywBusinessCar.getId());
    }
    
}
